<?php
	$setupdata['send_error_to'] = 'bugz@justpageit.lt';
	$setupdata['vars']['help'] = 1;

	error_reporting(E_ALL ^ E_NOTICE);

 	if (!isset($_GET['auth'])) $auth=NULL; else $auth=$_GET['auth'];
	if ($auth==NULL) if (!isset($_POST['auth'])) $auth=NULL; else $auth=$_POST['auth'];
 	if (!isset($_GET['admin'])) $admin=NULL; else $admin=$_GET['admin'];
	if ($admin==NULL) if (!isset($_POST['admin'])) $admin=NULL; else $admin=$_POST['admin'];

	include $setupdata['core_dir']."/setup.php";
	
	include $setupdata['core_dir']."/classes/mysql.php"; $mysqlclass = new MysqlClass;
	$mysqlclass->connect_db($setupdata['HostName'], $setupdata['UserName'], $setupdata['Password'], $setupdata['DBName']);
	
	include $setupdata['core_dir']."/classes/errors.php"; $err = new ErrorsClass;
	include $setupdata['core_dir']."/classes/classes.php"; $treeclass = new TreeCatalog;


	if(function_exists("register_shutdown_function")){
		#register_shutdown_function(array ('TreeCatalog', 'shutdownscript'));
	}
	
	if(!function_exists('http_build_query')) {
		function http_build_query($data, $prefix=null, $sep='', $key='') {
			$ret = array();
			foreach((array)$data as $k => $v) {
				$k = urlencode($k);
				if(is_int($k) && $prefix != null) $k = $prefix.$k;
				if(!empty($key)) $k = $key."[".$k."]";
				if(is_array($v) || is_object($v)) array_push($ret, http_build_query($v, "", $sep, $k));
				else array_push($ret,$k."=".urlencode($v));
			}
			if(empty($sep)) $sep = ini_get("arg_separator.output");
			return implode($sep, $ret);
		}
	}

	$is_xajax = false;
	if ((isset($admin)) or ($admin == "1")){
		include $setupdata['core_dir']."/xajax/xajax.inc.php";
		$xajax = new xajax($setupdata['base_href']."/index.php?".http_build_query($_GET));
		$xajax->cleanBufferOn();
		$xajax->bDebug = false;
		$xajax->registerExternalFunctionDir($setupdata['core_dir']."/xajax/plugins");
		$xajax->registerExternalFunctionDir($setupdata['xajax_user_plugins_dir']);
		$is_xajax = true;
	} else {
		if (isset($setupdata['xajax'])){
			include $setupdata['core_dir']."/xajax/xajax.inc.php";
			$xajax = new xajax($setupdata['base_href']."/index.php?corexajax=1");
			$xajax->cleanBufferOn();
			$xajax->bDebug = false;
			$xajax->registerExternalFunctionDir($setupdata['xajax_user_plugins_dir']);
			$is_xajax = true;
		}
	}

	include $setupdata['core_dir']."/classes/session.php"; $ses = new Sessions;
	include $setupdata['core_dir']."/classes/images.php"; $imageclass = new ImagesClass;
	include $setupdata['core_dir']."/classes/files.php"; $fileclass = new FilesClass;
	include $setupdata['core_dir']."/classes/users.php"; $usersclass = new UsersClass;
	include $setupdata['core_dir']."/classes/xml.php"; $xmlclass = new XmlClass;
	
	require $setupdata['core_dir']."/smarty/Smarty.class.php";
	
	require $setupdata['core_dir']."/ext/smarty_ext.php"; $smarty_ext=new smarty_ext;

	if (isset($setupdata['mysql']['character'])){
		$mysqlclass->mysql_my_query("SET CHARACTER SET ".$setupdata['mysql']['character'], __LINE__, __FILE__);
		$mysqlclass->mysql_my_query("SET NAMES ".$setupdata['mysql']['character'], __LINE__, __FILE__);
	}
	


 	$system_id = 9999999;

	if (session_id() == "") {
		session_name("tvs_session_id");
		session_start();
	}

	//if (!preg_match('#^[a-z0-9]$#i', session_id())) @session_regenerate_id();
	$ses->save_ses();

	if ($admin!=NULL)
	{
		if ($_COOKIE['interface_lang'])
		include $setupdata['core_dir']."/translate_".$_COOKIE['interface_lang'].".php";
		else
		include $setupdata['core_dir']."/translate_lt.php";
	}
	elseif (isset($setupdata["modules"]["translate"]["cache"]))
	{
		$translate = array();
		$translate = unserialize( file_get_contents( $setupdata['site_dir']."/translate.php" ) );
	}
	
	$smarty = new Smarty;
	$smarty->compile_dir = $setupdata['smarty_compile_dir'];
	$smarty->config_dir = $setupdata['core_dir']."/smarty/configs";
	$smarty->cache_dir = $setupdata['smarty_cache_dir'];
	$smarty->register_object("ext", $smarty_ext);
	$smarty->register_object("error", $err);
	$smarty->debugging = false;
		
	if (isset($setupdata['smarty_ext_template_dir'])){
		$smarty->ext_template_dir = $setupdata['smarty_ext_template_dir'];
	}

	if (!isset($setupdata['smarty_user_plugins_dir'])) $setupdata['smarty_user_plugins_dir'] = 'plugins/smarty';
	if (is_dir($setupdata['site_dir'].$setupdata['smarty_user_plugins_dir'])) $smarty_plugins_dirs[] = $setupdata['site_dir'].$setupdata['smarty_user_plugins_dir'];
	elseif (is_dir(getcwd().'/'.$setupdata['smarty_user_plugins_dir'])) $smarty_plugins_dirs[] = getcwd().'/'.$setupdata['smarty_user_plugins_dir'];
	elseif ($setupdata['smarty_user_plugins_dir']) $smarty_plugins_dirs[] = $setupdata['smarty_user_plugins_dir']; 

	if (is_dir($setupdata['core_dir'].'/smarty/tvs_plugins')) $smarty_plugins_dirs[] = $setupdata['core_dir'].'/smarty/tvs_plugins';
	$smarty_plugins_dirs[] = $setupdata['core_dir'].'/smarty/plugins';
	$smarty->plugins_dir = $smarty_plugins_dirs;

	if (isset($_GET['clear_smarty_cache']) && ($_GET['clear_smarty_cache'] == 1)){
		$smarty->clear_all_cache();
		$smarty->clear_compiled_tpl();
		print_r("CLEAR CACHE");
	}
	function smarty_block_dynamic($param, $content, &$smarty) {
		return $content;
	}
	function strip($tpl_output, &$smarty){    
		return preg_replace('!\s+!', ' ', $tpl_output);
	} 

	if (isset($setupdata['smarty_force_compile'])) $smarty->force_compile = $setupdata['smarty_force_compile'];
	else $smarty->force_compile = false;
	if (isset($setupdata['smarty_cache_modified_check'])) $smarty->cache_modified_check = $setupdata['smarty_cache_modified_check'];
	else $smarty->cache_modified_check = true;
	
	$smarty_ext->smarty=$smarty;
	
	if (isset($setupdata['modules'])){
		if (isset($setupdata['modules']['plugins']['path'])){
			$plugin_dir_tmp = explode("/", $setupdata['modules']['plugins']['path']);
			unset($plugin_dir_tmp[count($plugin_dir_tmp)-1]);
			$plugin_dir = implode("/", $plugin_dir_tmp);
		}
		foreach ($setupdata['modules'] as $key=>$sub_array) {
			if (isset($setupdata['modules']['plugins']['path']) && file_exists($plugin_dir."/modules/".$key)){
				$dir = $plugin_dir."/modules/".$key;
				if ($is_xajax !== false) $xajax->registerExternalFunctionDir($dir."/xajax");
				if (file_exists($dir."/".$key."class.php")) include $dir."/".$key."class.php";
				if (file_exists($dir."/smarty_ext_".$key.".php")) require $dir."/smarty_ext_".$key.".php";
			} else {
				$dir = $setupdata['core_dir']."/modules/".$key;
				if ($is_xajax !== false) $xajax->registerExternalFunctionDir($dir."/xajax");
				if (file_exists($dir."/".$key."class.php")) include $dir."/".$key."class.php";
				if (file_exists($dir."/smarty_ext_".$key.".php")) require $dir."/smarty_ext_".$key.".php";
			}
			$var = $key."class"; eval("\$$var = new $var;"); 
			$var = "smarty_ext_".$key; eval("\$$var = new $var;");
		}
	}
	if (isset($setupdata['modules']['plugins']['path'])){
		$plugins = $pluginsclass->get_all_plugins();
		foreach ($plugins as $plugin) {
			$dir = $setupdata['modules']['plugins']['path']."/".$plugin;
			if (file_exists($dir."/config.php")) include $dir."/config.php";
			if (file_exists($dir."/class.php")) include $dir."/class.php";
			if (file_exists($dir."/smarty.php")) require $dir."/smarty.php";
			$var = "plugin_class_".$plugin; eval("\$$var = new $var;"); 
			$var = "plugin_smarty_".$plugin; eval("\$$var = new $var;");
			$smarty->register_object($var, $$var);
		}
	}
	
	if (!isset($_GET['aerr'])) $aerr=NULL; else $aerr=$_GET['aerr']; 
	if ($aerr==NULL) if (!isset($_POST['aerr'])) $aerr=NULL; else $aerr=$_POST['aerr']; 
	if (!isset($_GET['inspic'])) $inspic=NULL; else $inspic=$_GET['inspic']; 
	if ($inspic==NULL) if (!isset($_POST['inspic'])) $inspic=NULL; else $inspic=$_POST['inspic']; 
	if (!isset($_GET['instab'])) $instab=NULL; else $instab=$_GET['instab']; 
	if ($instab==NULL) if (!isset($_POST['instab'])) $instab=NULL; else $instab=$_POST['instab']; 
	if (!isset($_GET['insfil'])) $insfil=NULL; else $insfil=$_GET['insfil']; 
	if ($insfil==NULL) if (!isset($_POST['insfil'])) $insfil=NULL; else $insfil=$_POST['insfil']; 
	if (!isset($_GET['insflash'])) $insflash=NULL; else $insflash=$_GET['insflash']; 
	if ($insflash==NULL) if (!isset($_POST['insflash'])) $insflash=NULL; else $insflash=$_POST['insflash']; 
	if (!isset($_GET['copys'])) $copys=NULL; else $copys=$_GET['copys']; 
	if ($copys==NULL) if (!isset($_POST['copys'])) $copys=NULL; else $copys=$_POST['copys']; 
	if (!isset($_GET['inslnk'])) $inslnk=NULL; else $inslnk=$_GET['inslnk']; 
	if ($inslnk==NULL) if (!isset($_POST['inslnk'])) $inslnk=NULL; else $inslnk=$_POST['inslnk']; 


	$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."local_vars`", __LINE__, __FILE__);
	while ($row = $mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {$setupdata['local_vars'][$row['name']] = $row['value'];}

	if ($auth != NULL) include $setupdata['core_dir']."/includes/auth.php";

	if (isset($setupdata['modules']['clients']['is'])) {
		if (isset($_COOKIE['client_username']) && isset($_COOKIE['client_password']) && (strlen($_COOKIE['client_username'])>0) && (strlen($_COOKIE['client_password'])>0)){
			$result = $mysqlclass->mysql_my_query("SELECT `parent` FROM `".$setupdata['table_prefix']."client_data` WHERE `name`='id_login' && '".$_COOKIE['client_username']."'=`value`", __LINE__, __FILE__);
			if ($mysqlclass->mysql_my_num_rows($result) > 0){
				$row1 = $mysqlclass->mysql_my_fetch_array($result, MYSQL_ASSOC);
				$result = $mysqlclass->mysql_my_query("SELECT `value` FROM `".$setupdata['table_prefix']."client_data` WHERE `name`='id_pass' && `parent`=".$row1['parent'], __LINE__, __FILE__);			
				if ($mysqlclass->mysql_my_num_rows($result) == 1){
					$row2 = $mysqlclass->mysql_my_fetch_array($result, MYSQL_ASSOC);
					if (strtoupper($row2['value']) == $_COOKIE['client_password']) {
						$_SESSION['client_login_id'] = $row1['parent'];
						$mysqlclass->mysql_my_query("DELETE FROM `".$setupdata['table_prefix']."sessions` WHERE `sesid` = '".session_id()."' AND `status`='client'", __LINE__, __FILE__);
						$mysqlclass->mysql_my_query("DELETE FROM `".$setupdata['table_prefix']."sessions` WHERE `userid` = '".$_SESSION['client_login_id']."'", __LINE__, __FILE__);
						$mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."sessions` values('".session_id()."', '".$_SESSION['client_login_id']."', '".$_SERVER['REMOTE_ADDR']."', '".date("YmdHis")."', '".date("YmdHis")."', 'client')", __LINE__, __FILE__);
						$_SESSION['client_havesess'] = 1;
						$ses->save_ses();
					} else {
						setcookie ("client_username", "", time() - 3600);
						setcookie ("client_password", "", time() - 3600);
					}
				} else {
					setcookie ("client_username", "", time() - 3600);
					setcookie ("client_password", "", time() - 3600);
				}
			}
		}
	}

	if ((isset($admin)) or ($admin == "1")){
		$result = $mysqlclass->mysql_my_query("SELECT `userid`, `status` FROM `".$setupdata['table_prefix']."sessions` WHERE `sesid`='".session_id()."' AND `status`='admin'", __LINE__, __FILE__);
		list($w_u_a, $w_status_a) = $mysqlclass->mysql_my_fetch_row($result);
		if ((!isset($_SESSION['login_user'])) or ((($_SESSION['login_user'] <> $w_u_a) or (!$w_u_a)) and ($_SESSION['login_user']<>$system_id))) {
			include $setupdata['core_dir']."/includes/_admin_.php";
		} else {
			if ($w_status_a == 'admin') {
				function login_ex() {}
				if ($inspic == "1") include $setupdata['core_dir']."/includes/inspic.php";
				elseif ($insfil == "1") include $setupdata['core_dir']."/includes/insfil.php";
				elseif ($insflash == "1") include $setupdata['core_dir']."/includes/insflash.php";
				elseif ($copys == "1") include $setupdata['core_dir']."/includes/copy.php";
				else include $setupdata['core_dir']."/includes/_admin_.php";
			} else include $setupdata['core_dir']."/includes/_admin_.php";
		}

	} else {
		if (isset($setupdata['modules']['clients']['is'])) {
			$result = $mysqlclass->mysql_my_query("SELECT `userid`, `status` FROM `".$setupdata['table_prefix']."sessions` WHERE `sesid`='".session_id()."' AND `status`='client'", __LINE__, __FILE__);
			list($w_u_c, $w_status_c) = $mysqlclass->mysql_my_fetch_row($result);
			if ((!isset($_SESSION['client_login_id'])) or ($_SESSION['client_login_id'] <> $w_u_c) or (!$w_u_c)) {
				$_SESSION['client_login_id'] = "";
				if (isset($_SESSION['client_havesess'])) unset($_SESSION['client_havesess']);
			} else $_SESSION['client_login_id'] = $w_u_c;
		}
		include $setupdata['core_dir']."/includes/_guest_.php";
	}

	if (!isset($_GET['show']) && (isset($_GET['benchmark']))) echo $treeclass->get_debug_info();
	$mysqlclass->close_db();
	exit;
?>
